package com.jinxin.platform.cdacscontrolmgr.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.api.annotation.APCOSApi;
import com.jinxin.platform.base.api.annotation.APCOSApiOperation;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdacscontrolmgr.bean.CdparkmanagerProjectBarrier;
import com.jinxin.platform.cdacscontrolmgr.common.Constant;
import com.jinxin.platform.cdacscontrolmgr.event.AcsControlmgrCarRecordReportListener;
import com.jinxin.platform.cdacscontrolmgr.service.ICdacsControlmgrProjectBarrierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-11-18
 * @Author: Lingnan
 **/
@APCOSApi(value = "测试接口", developers = {"lingnan"}, description = "测试接口")
@RestController
@RequestMapping("/cdacscontrolmgr/cdacscontrolmgr")
@Slf4j
public class CdacscontrolmgrTestController {
    @Autowired
    private ICdacsControlmgrProjectBarrierService controlmgrProjectBarrierService;
    @Autowired
    private AcsControlmgrCarRecordReportListener controlmgrCarRecordReportListener;

    @APCOSApiOperation(value = "模拟上报", developers = {"lingnan"}, description = "模拟上报",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping(value = "/sendEvent/{serialNum}/{productCode}")
    public JsonResult sendEvent(@PathVariable("serialNum") String serialNum,@PathVariable("productCode") String productCode) {
        HashMap param = new HashMap();
        param.put("carMac",serialNum);
        CdparkmanagerProjectBarrier projectBarrier = controlmgrProjectBarrierService.selectOne(param);
        if (projectBarrier == null) {
            return JsonResult.error("无此设备");
        }
        //{data={"reportMessage":"{\"r2\":\"00\",\"r3\":\"03\",\"r1\":\"0100011800\"}","product_code":"00040101","serial_num":"0005e5010005e501","account":"1565100000001284","cmd_id":"15703","cmd_name":"device status"}}
        String eventType = "dev-cdreport";
        HashMap<String, Object> map = new HashMap<>();
        HashMap<String, Object> data = new HashMap<>();
        data.put("product_code", productCode);
        data.put("serial_num", serialNum);
        data.put("account", "admin");
        data.put("cmd_id", "15707");
        data.put("cmd_name", "Status query/Trigger instruction /control response/cdreport");

        //{"r2":"20200701100152","r3":"02","r1":"0100011800"}
        //{"r2":"0000000000000000000000","r3":"7c","r4":"00002046","r5":"a2","r6":"1a","r7":"16","r8":"00","r1":"00f0fe6bc250a06bc250a0"}
        HashMap<String, Object> message = new HashMap<>();
        message.put("r1", Constant.IOCTL_RESP_BXH);
        message.put("r2", "");
        message.put("r3", "闽E997BD");
        data.put("reportMessage", JSONObject.toJSONString(message));

        map.put("data", data);
        map.put("modules", "["+Constant.MODULE_KEY+"]");

        controlmgrCarRecordReportListener.event(map);
        return JsonResult.ok();
    }
}
